﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel.Syndication;
using System.Xml;
using PBDesk.Utilities;
using System.Xml.Linq;
using System.Collections.ObjectModel;

namespace PBDesk.Business.FeedProvider
{
    public class FeedHelper
    {
        public static SyndicationFeed GetFeeds(int channelId)
        {
            SyndicationFeed feeds = new SyndicationFeed();
            if (channelId > 0)
            {
                GetFeedsChannelData(channelId, ref feeds);
                feeds.Items = GetFeedsNewsItems(channelId);

            }
            return feeds;
        }

        public static void GetFeedsChannelData(int channelId, ref SyndicationFeed feeds)
        {            
            if(feeds != null)
            {
                string title, desc, link, imageUrl;
                DBMapper.GetFeedsChannelData(channelId, out title, out desc, out link, out imageUrl);
                feeds.Title = new TextSyndicationContent(title);
                feeds.Description = new TextSyndicationContent(desc);
                feeds.ImageUrl = new Uri(imageUrl);
                feeds.Links.Add(new SyndicationLink(new Uri(link)));
            }                            
        }

        public static IEnumerable<SyndicationItem> GetFeedsNewsItems(int channelId)
        {
            IEnumerable<SyndicationItem> list = new List<SyndicationItem>();
            list = DBMapper.GetFeedsNewsItems(channelId);
            return list;

        }

        public static SyndicationFeed GetExternalFeeds(string Url)
        {
            SyndicationFeed feed = new SyndicationFeed();
            Collection<SyndicationItem> returnList = new Collection<SyndicationItem>();
            if(Url.IsNotNullAndNotEmpty() && StringUtils.IsValidUrl(Url))
            {
                //XmlReader reader = XmlReader.Create(Url);
                XDocument xdoc = XDocument.Load(Url, LoadOptions.None);               
                if (xdoc != null)
                {                    
                    List<XElement> items = xdoc.Descendants("item").ToList<XElement>();
                    var feedNewsItems = from item in items
                                        select new FeedNewsItem
                                 {
                                     Title = item.Element(XName.Get("title")).Value,
                                     Description = item.Element(XName.Get("description")).Value,
                                     Link = item.Element(XName.Get("link")).Value

                                 };
                    if(feedNewsItems != null && feedNewsItems.Count() > 0)
                    {
                        foreach (FeedNewsItem feedNewsItem in feedNewsItems)
                        {
                            returnList.Add(new SyndicationItem(feedNewsItem.Title, feedNewsItem.Description, new Uri(feedNewsItem.Link)));
                        }
                    }

                    feed = new SyndicationFeed(returnList);
                    
                }
                //if (reader != null)
                //{
                //    feed = SyndicationFeed.Load(reader);
                //}

            }
            return feed;            
        }

        public static IEnumerable<SyndicationItem> GetMergedFeedItems(List<SyndicationFeed> feedList, int topCount = 0)
        {
            SyndicationFeed newFeed = new SyndicationFeed();
            List<SyndicationItem> newsItems = new List<SyndicationItem>();
            if (feedList != null && feedList.Count > 0)
            {
                foreach (SyndicationFeed feed in feedList)
                {
                    newsItems.AddRange(feed.Items);
                }
                newsItems.Sort(FeedComparerByPublishDate);
                if(topCount > 0)
                {
                    newsItems.Take(topCount);
                
                }
            }            
            return newsItems;
        }

        private static int FeedComparerByPublishDate(SyndicationItem one, SyndicationItem two)
        {
            return one.PublishDate.CompareTo(two.PublishDate);
        }




        internal static Dictionary<int, string> GetChannelList()
        {
            return DBMapper.GetChannelList();
        }
    }

    public class FeedNewsItem
    {
        public string Title { get; set; }
        public string Description { get; set; }
        public string Link { get; set; }
    }

}
